package cn.jiedh.app.meiweidk.Utils.UI;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import cn.jiedh.app.meiweidk.R;

/**
 * 创建人 PACY
 * 创建时间 18/11/16 16:47.
 * 邮箱 pengwwe457621023@qq.com
 * 类描述 组合view  用于左边标题右边值带右箭头类型  底部有分割线
 */

public class CustomItemLayout extends RelativeLayout {

    private TextView item_title;
    private TextView item_value;
    private ImageView item_lable;
    private TextView item_line;


    private String title;
    private int titleColor;

    private String value;
    private int valueColor;


    private int lableResouce;
    private boolean showLable;

    private boolean showLine;

    public CustomItemLayout(Context context) {
        super(context);
    }

    public CustomItemLayout(Context context, AttributeSet attrs) {
        super(context, attrs);

        //获取视图
        LayoutInflater.from(context).inflate(R.layout.custom_item_layout, this, true);
        //加载自定义属性
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomItemLayout);
        title = a.getString(R.styleable.CustomItemLayout_titleText);
        titleColor = a.getColor(R.styleable.CustomItemLayout_titleTextColor, context.getResources().getColor(R.color.lable_text_color));
        value = a.getString(R.styleable.CustomItemLayout_valueText);
        valueColor = a.getColor(R.styleable.CustomItemLayout_valueTextColor, Color.BLACK);
        lableResouce = a.getResourceId(R.styleable.CustomItemLayout_lableResouce, R.drawable.black_lable);
        showLable = a.getBoolean(R.styleable.CustomItemLayout_showLable, false);
        showLine = a.getBoolean(R.styleable.CustomItemLayout_showLine, true);
        //回收资源
        a.recycle();

    }

    /**
     * 视图渲染结束调用
     */
    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        item_title = findViewById(R.id.item_title);
        item_value = findViewById(R.id.item_value);
        item_lable = findViewById(R.id.item_lable);
        item_line = findViewById(R.id.item_line);


        if (!TextUtils.isEmpty(title) && !TextUtils.isEmpty(value)) {
            setTitle(title);
            setTitleColor(titleColor);
            setValue(value);
            setValueColor(valueColor);
            setLableResouce(lableResouce);
            setShowLable(showLable);
            setShowLine(showLine);
        } else {
            throw new NullPointerException("title or value is not empty");
        }

    }

    /**
     * 设置左边title文字
     *
     * @param text
     */
    public void setTitle(String text) {
        item_title.setText(text);
    }

    /**
     * 设置左边title文字颜色
     *
     * @param color
     */
    public void setTitleColor(int color) {
        item_title.setTextColor(color);
    }

    /**
     * 设置value文字
     */
    public void setValue(String text) {
        item_value.setText(text);
    }


    public String getValue() {
        return item_value.getText().toString();
    }

    /**
     * 设置value文字颜色
     *
     * @param color
     */
    public void setValueColor(int color) {
        item_value.setTextColor(color);
    }

    /**
     * 设置箭头资源
     */
    public void setLableResouce(int resouce) {
        item_lable.setImageResource(resouce);
    }

    /**
     * 设置箭头是否显示
     *
     * @param isShow
     */
    public void setShowLable(boolean isShow) {

        if (isShow) {
            item_lable.setVisibility(VISIBLE);
        } else {
            item_lable.setVisibility(GONE);
        }

    }

    /**
     * 设置分割线是否显示
     *
     * @param isShow
     */
    public void setShowLine(boolean isShow) {

        if (isShow) {
            item_line.setVisibility(VISIBLE);
        } else {
            item_line.setVisibility(GONE);
        }
    }

//    /**
//     * 设置点击监听
//     * @param listener
//     */
//    public void setOnClickListener(OnClickListener listener){
//        item_value.setOnClickListener(listener);
//    }
}
